<template>
  <base-breadcrumb>
    <a-card :bordered="false" style="over-flow:scroll">
      <title-name title="评价表信息"></title-name>
      <a-form>
        <a-row>
          <a-col :md="9" :sm="9">
            <a-form-item
              label="评价表名称"
              :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
              :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
            >
              <!--<BaseInput v-model="form.formName" :disabled="pageType=='view' || evaluationName.filter(()=>i == form.formName).length"-->
              <!--placeholder="请输入"  />-->
              <BaseInput v-model="form.formName" disabled placeholder="请输入" />
            </a-form-item>
          </a-col>
          <a-col :md="9" :sm="9">
            <a-form-item
              label="状态"
              :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
              :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
            >
              <a-select v-model="form.enable" disabled>
                <a-select-option :value=true>可用</a-select-option>
                <a-select-option :value=false>不可用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <title-name title="评分项列表"></title-name>
      <div>
        <a-row>
          <a-col :md="24" :sm="19">
            <a-table bordered :pagination="false"
                     @change="handleTableChange"
                     :columns="columns" :data-source="tableData"
            >
              <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
            </a-table>
          </a-col>
        </a-row>
      </div>
    </a-card>


    <div style="float:right;" class="page-btn-right-top">
      <a-button type="primary" @click="goBack">关闭</a-button>
    </div>

  </base-breadcrumb>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import {
  getScoreList,
  addScoreList,
  editScoreList,
  getEvaluateListTotal,
  deleteScoreList,
  editEvaluateList
} from '@/api/evaluate'
import { getProjectJobsList } from '@/api/project/projectsJobs'
import { getProjectIdFromOrg } from '@/api/index'
import cloneDeep from 'lodash.clonedeep'

import { getHeaders, getContent, getTypeLabel } from '@/api/commons'
import DynamicForm from '@/components/DynamicForm/index.js'

const searchArr = [
  // 搜索框
  {
    name: '评价表名称',
    value: 'recommendSubcontractors',
    type: 'input'
  },
  {
    name: '状态',
    type: 'select',
    value: 'status',
    data: [
      {
        value: '0',
        label: '可用'
      },
      {
        value: '1',
        label: '不可用'
      }
    ]
  }
]
export default {
  components: {
    STable,
    DynamicForm
  },
  data() {
    this.searchArr = searchArr
    return {
      efficiencys: [],
      resourcesEvaluation: [],
      loading: false,
      // 查询参数
      evaluationName: ['分包商月度考核评价表', '分包商季度考核评价表', '设计资源考核评价表', '设计策划编制评审表', '设计效能考核表', '专业工程施工方案讲解评价表', '施工组织设计(方案)编制评价表', '回填土专项治理查评分表', '工程渗漏专项治理查评分表', '混凝土观感专项治理查评分表'],
      queryParam: {},
      tableDataCopy: [],
      tableData: [],
      formDatas: [],
      defaultData: {},
      data: {},
      deparments: [], // 考核部门

      addForm: {},
      // 加载数据方法 必须为 Promise 对象
      selectedRowKeys: [],
      selectedRows: [],
      confirmLoading: false,
      columns: [],
      id: '',
      name: '',
      type: '',
      status: '',
      pageType: '',
      jobsList: [],

      classifications: [],  // 考核分类
      types: [],  // 考核类型

      list: [
        { id: 1, name: '分包商月度考核评价表' },
        { id: 2, name: '分包商季度考核评价表' },
        { id: 3, name: '设计策划编制评审表' },
        { id: 4, name: '设计效能考评表' },
        { id: 5, name: '设计资源考核评价表' },
        { id: 6, name: '专业工程施工方案讲解评价表' },
        { id: 7, name: '施工组织设计（方案）编制评价表' },
        { id: 8, name: '回填土专项治理查评分表' },
        { id: 9, name: '工程渗漏专项治理查评分表' },
        { id: 10, name: '混凝土观感专项治理查评分表' }
      ],
      form: {
        id: '',
        formName: '',
        enable: ''
      },
      visible: false
    }
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        selectedRows: this.selectedRows,   // 当前行所有数据
        onChange: this.onSelectChange
      }
    }
  },
  created() {
    // this.getProjectJobsList()
    this.getProjectIdFromOrg()
    this.id = this.$route.query.id
    this.form.formName = this.$route.query.name
    if (this.form.formName == '分包商月度考核评价表') {
      this.type = 1
    } else if (this.form.formName == '分包商季度考核评价表') {
      this.type = 2
    } else if (this.form.formName == '设计策划编制评审表') {
      this.type = 3
    } else if (this.form.formName == '设计效能考核表') {
      this.type = 4
    } else if (this.form.formName == '设计资源考核评价表') {
      this.type = 5
    } else if (this.form.formName == '专业工程施工方案讲解评价表') {
      this.type = 6
    } else if (this.form.formName == '施工组织设计(方案)编制评价表') {
      this.type = 7
    } else if (this.form.formName == '回填土专项治理查评分表') {
      this.type = 8
    } else if (this.form.formName == '工程渗漏专项治理查评分表') {
      this.type = 9
    } else if (this.form.formName == '混凝土观感专项治理查评分表') {
      this.type = 10
    }
    let status = this.$route.query.status

    this.form.enable = status == '可用' ? true : false

    this.pageType = this.$route.query.type
    this.getHeaders()
    this.getContent()
    this.getTypeLabel()
  },
  methods: {
    // 获取分公司下的部门
    getProjectIdFromOrg() {
      this.loading = true
      getProjectIdFromOrg({ parentCodes: ['PD'], pageNum: 1, pageSize: 50, baseOrgType: 1 }).then(res => {
        console.log('99999:', res.data.list)
        this.loading = false
        this.deparments = res.data.list || []
        this.formDatas.forEach(item => {
          if (item.code === '9') {
            item.selectList = this.deparments
          }
        })
      }).catch(() => {
        this.loading = false
      })
    },

    getProjectJobsList(callack) {
      this.loading = true
      getProjectJobsList({ pageNo: 1, pageSize: 10 }).then((res) => {
        this.loading = false
        if (res.code === 200 || res.data.code === 200) {
          this.jobsList = res.data || []
          this.formDatas.forEach(item => {
            if (item.code === '5') {
              item.selectList = this.jobsList
            }
          })
          callack && callack()
        }
      }).catch(() => {
        this.loading = false
      })
    },

    getTypeLabel() {

      this.loading = true
      getTypeLabel({ type: 3 }).then(res => {
        this.loading = false
        if (res.code === 200 || res.data.code === 200) {
          this.classifications = res.data
          this.formDatas.forEach(item => {
            if (item.code === '3') {
              item.selectList = this.classifications
            }
          })
        }
      }).catch(() => {
        this.loading = false
      })

      // 考核分类
      //设计效能考核评价表
      if (this.type == 4) {
        getTypeLabel({ type: 5 }).then(res => {
          this.loading = false
          if (res.code === 200 || res.data.code === 200) {
            this.efficiencys = res.data
            this.formDatas.forEach(item => {
              if (item.code === '2') {
                item.selectList = this.efficiencys
              }
            })
          }

        }).catch(() => {
          this.loading = false
        })
      }

      // 设计资源考核评价表
      if (this.type == 5) {
        getTypeLabel({ type: 6 }).then(res => {
          this.loading = false
          if (res.code === 200 || res.data.code === 200) {
            this.resourcesEvaluation = res.data
            this.formDatas.forEach(item => {
              if (item.code === '2') {
                item.selectList = this.resourcesEvaluation
              }
            })
          }
        }).catch(() => {
          this.loading = false
        })
      }
    },

    getHeaders() {
      let _this = this
      getHeaders({ formId: this.id }).then(res => {
        _this.formDatas = cloneDeep(res.data)
        _this.formDatas.forEach(function(obj, i) {

          if (obj.code == '2' && obj.inputType == 'SELECT' && _this.type == 4) {
            obj.selectList = _this.efficiencys
            obj.selectOptions = { labelName: 'name', valueName: 'id' }
          }

          if (obj.code == '2' && obj.inputType == 'SELECT' && _this.type == 5) {
            obj.selectList = _this.resourcesEvaluation
            obj.selectOptions = { labelName: 'name', valueName: 'id' }
          }

          if (obj.code == '3') {
            obj.$listeners = {
              change: (val) => {
                _this.formDatas = _this.formDatas.map(item => {
                  if (item.code === '4') {
                    if (val === '625250987615064064') {
                      item.hidden = true
                    } else {
                      item.hidden = false
                    }
                  }
                  return item
                })
              }
            }

            obj.selectList = _this.classifications
            obj.selectOptions = { labelName: 'name', valueName: 'id' }
          }

          if (obj.code == '5') {
            obj.selectList = _this.jobsList
            obj.selectOptions = { labelName: 'jobName', valueName: 'jobId' }
          }

          if (obj.code == '9') {
            obj.selectList = _this.deparments
            obj.selectOptions = { labelName: 'name', valueName: 'id' }
          }

          if (obj.code == '8') {
            obj.selectList = [{ label: '不可用', value: '不可用' }, { label: '可用', value: '可用' }]
          }
        })
        res.data && res.data.forEach(item => item.ellipsis = true)
        res.data.map(v => {
          if (v.title === '排序') {
            v.sorter = true
          }
        })
        _this.columns = res.data
        let obj = {
          'dataIndex': 'serial',
          'formId': null,
          'title': '序号',
          'code': '0',
          'sort': 0,
          'inputType': null,
          scopedSlots: { customRender: 'serial' }
        }
        _this.columns.unshift(obj)
      })
    },
    handleTableChange(pagination, filters, sorter) {
      console.log('sorter', sorter)
      if (sorter.order === 'ascend') {
        this.tableData.sort((a, b) => {
          if (!a[sorter.field]) {
            return 1
          }
          if (!b[sorter.field]) {
            return -1
          }
          if (a[sorter.field] < b[sorter.field]) return 1
          if (a[sorter.field] > b[sorter.field]) return -1
          return 0
        })
        return
      }
      if (sorter.order === 'descend') {
        this.tableData.sort((a, b) => {
          if (!a[sorter.field]) {
            return -1
          }
          if (!b[sorter.field]) {
            return 1
          }
          if (a[sorter.field] < b[sorter.field]) return -1
          if (a[sorter.field] > b[sorter.field]) return 1
          return 0
        })
        return
      }
      this.tableData = this.$clone(this.tableDataCopy)
    },
    submit() {   // 提交
      this.form.id = this.id
      editEvaluateList(this.form).then(res => {
        if (res.code == 200) {
          this.$message.success('提交成功')
          this.$router.push({ path: '/project/evalution/list' })
        }
      })
    },
    goBack() {   // 关闭
      this.$router.push({ path: '/project/evalution/list' })
    },
    handleOk() {
      const form = this.$refs.dynamicForm.getFormInfo()
      let arr = []
      for (let k in form) {
        if (form.hasOwnProperty(k)) {
          if (k != 'id') {
            arr.push({
              headerItemId: k,
              value: form[k],
              veto: true
            })
          }

        }
      }
      this.addForm.formId = this.id
      this.addForm.itemChild = arr

      if (this.defaultData.id) {
        this.addForm.id = this.defaultData.id
        editScoreList(this.addForm).then((res) => {
          if (res.code == '200') {
            this.$message.success('编辑成功')
            this.visible = false
            this.getContent()
          }
        })
      } else {
        addScoreList(this.addForm).then((res) => {
          if (res.code == '200') {
            this.$message.success('新增成功')
            this.getContent()
            this.visible = false
          }
        })
      }

    },
    getContent() {
      getContent({ formId: this.id }).then(res => {
        this.tableData = this.$clone(res.data)
        this.tableDataCopy = this.$clone(res.data)
      })
    },


    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    search() {
      this.$refs.table.refresh(true)
    },
    goAdd() {
      if (this.loading) return
      this.getProjectJobsList(() => {
        this.defaultData = {}
        this.visible = true
      })
    },
    goDel() {
      let _this = this
      this.$confirm({
        okText: '删除',
        okType: 'danger',
        cancelText: '取消',
        title: '删除当前选择的评分项?',
        onOk() {
          let data = []
          _this.selectedRows.forEach(item => {
            data.push(item.id)
          })
          deleteScoreList({ ids: data }).then((res) => {
            if (res.data.code == '200') {
              _this.getContent()
              _this.$message.success('删除成功')
            }
            if (res) {
              _this.$refs.table.refresh(true)
            }
          })
        },
        onCancel() {
        }
      })

    },
    handleSub(record, type) {

    },
    handleEdit(record) {
      this.visible = true
      this.defaultData = record
    },

    handleCancel() {
      this.visible = false
    }
  }
}
</script>

<style scoped>
.input {
  width: 100%
}
</style>
